package com.ququ.syb.account.controller;

import com.ququ.common.result.Result;
import com.ququ.syb.account.service.AccountInfoService;
import com.ququ.syb.common.pojo.AccountInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 生意宝账户管理模块
 *
 * @author Macro
 * @date 2019-08-21
 *
 */
@RestController
public class AccountInfoController {
    @Autowired
    private AccountInfoService accountInfoService;

    /**
     * 查询生意宝账户
     *
     * @param accountid 生意宝账户|必填
     * @return 当前生意宝账户基本信息
     * @respbody {"data":{"accountid":"100000000064","accountname":"北斗茶馆","allowpay":1,"allowwithdraw":1,"appcode":"100004","createtime":1553020059000,"id":1,"lastmodifytime":1553104589000,"mobile":"13950280566","name":"张宏","password":"fc2e5126e75ab164ae599aecf39672aa","status":1,"token":"f4245ac2e14d1b981fa2771cfc3f48fd"},"msg":"成功","page":{"count":1,"index":1,"size":10,"total":1},"status":1}
     * @resp status 状态值，1-成功 其他值-失败|int
     * @resp msg 错误信息|string
     * @resp data 生意宝账户对象|AccountInfo
     * @resp data.id 序号|int
     * @resp data.status 账户状态 1-正常 0-冻结|int
     * @resp data.name 姓名|string
     * @resp data.mobile 手机号|string
     * @resp data.accountid 生意宝账号id|string
     * @resp data.accountname 账户名|string
     * @resp data.appcode 归属应用代码|string
     * @resp data.partner 归属渠道商账号|string
     * @resp data.idcardno 身份证号|string
     * @resp data.createtime 注册时间|string
     * @resp data.lastmodifytime 更新时间|string
     * @resp data.allowpay 允许支付 1-允许 0-禁止|int
     * @resp data.allowwithdraw 允许提现 1-允许 0-禁止|int
     * @resp data.token 通信密钥|string
     * @resp data.password  交易密码|string
     * @resp page 分页信息，可忽略|object
     */
    @RequestMapping("/query")
    public Result<AccountInfo> getAccountInfo(@RequestParam String accountid){
        AccountInfo accountInfo= accountInfoService.getAccountInfo(accountid);
        if (accountInfo == null)
            return new Result<>(-1,"无此生意宝账户");
        else 
            return new Result<>(accountInfo);
    }


    /**
     * 根据手机号或身份证号查询生意宝账号列表
     *
     * @param mobile 手机号|string
     * @param idcardno 身份证号|string
     * @param status 账户状态|int
     * @return 符合条件的生意宝账户列表
     * @respbody {"data":[{"accountid":"100000000064","accountname":"北斗茶馆","allowpay":1,"allowwithdraw":1,"appcode":"100004","createtime":1553020059000,"id":1,"lastmodifytime":1553104589000,"mobile":"13950280566","name":"张宏","password":"fc2e5126e75ab164ae599aecf39672aa","status":1,"token":"f4245ac2e14d1b981fa2771cfc3f48fd"}],"msg":"成功","page":{"count":1,"index":1,"size":10,"total":1},"status":1}
     * @resp status 状态值，1-成功 其他值-失败|int
     * @resp msg 错误信息|string
     * @resp data 生意宝账户对象|List<AccountInfo>
     * @resp data.id 序号|int
     * @resp data.status 账户状态 1-正常 0-冻结|int
     * @resp data.name 姓名|string
     * @resp data.mobile 手机号|string
     * @resp data.accountid 生意宝账号id|string
     * @resp data.accountname 账户名|string
     * @resp data.appcode 归属应用代码|string
     * @resp data.partner 归属渠道商账号|string
     * @resp data.idcardno 身份证号|string
     * @resp data.createtime 注册时间|string
     * @resp data.lastmodifytime 更新时间|string
     * @resp data.allowpay 允许支付 1-允许 0-禁止|int
     * @resp data.allowwithdraw 允许提现 1-允许 0-禁止|int
     * @resp data.token 通信密钥|string
     * @resp data.password  交易密码|string
     * @resp page 分页信息，可忽略|object
     *
     */
    @RequestMapping("/list")
    public Result<List<AccountInfo>> listAccountInfo(@RequestParam(required = false) String mobile,
                                                     @RequestParam(required = false) String idcardno,
                                                     @RequestParam(required = false) Integer status){
        List<AccountInfo> accountInfoList= accountInfoService.listAccountInfo(mobile, idcardno, status);
        if (accountInfoList == null || accountInfoList.size() == 0)
            return new Result<>(-1,"无此生意宝账户");
        else
            return new Result<>(accountInfoList);
    }

    /**
     * 保存生意宝账户
     *
     * @param accountid 生意宝账号id|必填
     * @param name 姓名
     * @param mobile 手机号|必填
     * @param accountname 账户名
     * @param appcode 应用代码
     * @param partner 渠道商户号
     * @param idcardno 身份证号
     * @param allowpay 允许支付
     * @param allowwithdraw 允许提现
     * @param token 通信密钥
     * @param password 交易密码
     * @return 保存的生意宝账户记录
     * @respbody {"data":{"accountid":"100000000064","accountname":"北斗茶馆","allowpay":1,"allowwithdraw":1,"appcode":"100004","createtime":1553020059000,"id":1,"lastmodifytime":1553104589000,"mobile":"13950280566","name":"张宏","password":"fc2e5126e75ab164ae599aecf39672aa","status":1,"token":"f4245ac2e14d1b981fa2771cfc3f48fd"},"msg":"成功","page":{"count":1,"index":1,"size":10,"total":1},"status":1}
     * @resp status 状态值，1-成功 其他值-失败|int
     * @resp msg 错误信息|string
     * @resp data 生意宝账户对象|AccountInfo
     * @resp data.id 序号|int
     * @resp data.status 账户状态 1-正常 0-冻结|int
     * @resp data.name 姓名|string
     * @resp data.mobile 手机号|string
     * @resp data.accountid 生意宝账号id|string
     * @resp data.accountname 账户名|string
     * @resp data.appcode 归属应用代码|string
     * @resp data.partner 归属渠道商账号|string
     * @resp data.idcardno 身份证号|string
     * @resp data.createtime 注册时间|string
     * @resp data.lastmodifytime 更新时间|string
     * @resp data.allowpay 允许支付 1-允许 0-禁止|int
     * @resp data.allowwithdraw 允许提现 1-允许 0-禁止|int
     * @resp data.token 通信密钥|string
     * @resp data.password  交易密码|string
     * @resp page 分页信息，可忽略|object
     */
    @RequestMapping("/save")
    public Result<AccountInfo> saveAccountInfo(@RequestParam String accountid,
                                               @RequestParam(required = false) String name,
                                               @RequestParam String mobile,
                                               @RequestParam(required = false) String accountname,
                                               @RequestParam(required = false) String appcode,
                                               @RequestParam(required = false) String partner,
                                               @RequestParam(required = false) String idcardno,
                                               @RequestParam(required = false) Integer allowpay,
                                               @RequestParam(required = false) Integer allowwithdraw,
                                               @RequestParam(required = false) String token,
                                               @RequestParam(required = false) String password){
        AccountInfo accountInfo = accountInfoService.getAccountInfo(accountid);
        if (accountInfo != null)
            return new Result<>(-1,"账户"+accountid+"已经注册");
        accountInfoService.save(accountid, name, mobile, accountname, appcode, partner, idcardno, allowpay, allowwithdraw, token, password);
        accountInfo= accountInfoService.getAccountInfo(accountid);
        return new Result<>(accountInfo);
    }


    /**
     * 更新生意宝账户
     *
     * @param accountid 生意宝账号id|必填
     * @param name 姓名
     * @param mobile 手机号
     * @param accountname 账户名
     * @param appcode 应用代码
     * @param partner 渠道商户号
     * @param idcardno 身份证号
     * @param allowpay 允许支付
     * @param allowwithdraw 允许提现
     * @param token 通信密钥
     * @param password 交易密码
     * @return 更新后的生意宝账户记录
     * @respbody {"data":{"accountid":"100000000064","accountname":"北斗茶馆","allowpay":1,"allowwithdraw":1,"appcode":"100004","createtime":1553020059000,"id":1,"lastmodifytime":1553104589000,"mobile":"13950280566","name":"张宏","password":"fc2e5126e75ab164ae599aecf39672aa","status":1,"token":"f4245ac2e14d1b981fa2771cfc3f48fd"},"msg":"成功","page":{"count":1,"index":1,"size":10,"total":1},"status":1}
     * @resp status 状态值，1-成功 其他值-失败|int
     * @resp msg 错误信息|string
     * @resp data 生意宝账户对象|AccountInfo
     * @resp data.id 序号|int
     * @resp data.status 账户状态 1-正常 0-冻结|int
     * @resp data.name 姓名|string
     * @resp data.mobile 手机号|string
     * @resp data.accountid 生意宝账号id|string
     * @resp data.accountname 账户名|string
     * @resp data.appcode 归属应用代码|string
     * @resp data.partner 归属渠道商账号|string
     * @resp data.idcardno 身份证号|string
     * @resp data.createtime 注册时间|string
     * @resp data.lastmodifytime 更新时间|string
     * @resp data.allowpay 允许支付 1-允许 0-禁止|int
     * @resp data.allowwithdraw 允许提现 1-允许 0-禁止|int
     * @resp data.token 通信密钥|string
     * @resp data.password  交易密码|string
     * @resp page 分页信息，可忽略|object
     */
    @RequestMapping("/update")
    public Result<AccountInfo> updateAccountInfo(@RequestParam(required = false) Integer id,
                                                 @RequestParam String accountid,
                                                 @RequestParam(required = false) String name,
                                                 @RequestParam(required = false) String mobile,
                                                 @RequestParam(required = false) String accountname,
                                                 @RequestParam(required = false) String appcode,
                                                 @RequestParam(required = false) String partner,
                                                 @RequestParam(required = false) String idcardno,
                                                 @RequestParam(required = false) Integer allowpay,
                                                 @RequestParam(required = false) Integer allowwithdraw,
                                                 @RequestParam(required = false) String token,
                                                 @RequestParam(required = false) String password,
                                                 @RequestParam(required = false) Integer status){
        AccountInfo accountInfo = accountInfoService.getAccountInfo(accountid);
        if (accountInfo == null)
            return new Result<>(-1,"账户"+accountid+"尚未注册");
        accountInfo = accountInfoService.update(accountInfo.getId(), accountid, name, mobile, accountname, appcode, partner, idcardno, allowpay, allowwithdraw, token, password,status);
        return new Result<>(accountInfo);
    }

}
